package com.mybatis.panel;

import com.mybatis.entity.AwardPunish;
import com.mybatis.entity.User;
import com.mybatis.service.AwardPunishService;
import com.mybatis.service.UserService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class AwardInsertPanel extends JPanel {

    public static int pageSize = 1;

    public static String dataRow;

    public AwardInsertPanel() {
        setLayout(null);
        setBounds(0, 0, 300, 300);

        //===========================================
//        JPanel panel = new JPanel(new BorderLayout());
//        panel.setBounds(0, 0, 900, 600);//位置
//        panel.add(new JLabel("用户名"));//文字


        JPanel panel2 = new JPanel();
        panel2.setBounds(0, 140, 0, 0);
        add(panel2);

        JLabel jLabel3 = new JLabel();
        jLabel3.setText("学号:");
        jLabel3.setBounds(50, 50, 50, 50);
        add(jLabel3);

        JLabel jLabel4 = new JLabel();
        jLabel4.setBounds(30, 75, 70, 50);
        jLabel4.setText("奖惩情况:");


        //文本框
        JTextField jTextField = new JTextField(10);
        jTextField.setBounds(100, 65, 100, 20);
        add(jTextField);

        add(jLabel4);

        JTextField passwordField = new JTextField(10);
        passwordField.setBounds(100, 90, 100, 20);
        add(passwordField);

        ButtonGroup group = new ButtonGroup();

//        JRadioButton radioButton1 = new JRadioButton("管理员", false);
//        radioButton1.setBounds(70, 120, 20, 20);
//        add(radioButton1);
//        JRadioButton radioButton2 = new JRadioButton("普通用户",true);
//        radioButton1.setBounds(60, 120, 20, 20);
        //group.add(radioButton1);
//        JLabel usertype = new JLabel("www");
//        usertype.setText("管理员");
//        usertype.setBounds(90, 120, 20, 20);
//        add(usertype);


        JButton loginButton = new JButton("添加");
        //loginButton.setText("www");
        loginButton.setBounds(70, 150, 40, 30);
        loginButton.setMargin(new Insets(0, 0, 0, 0));
        add(loginButton, BorderLayout.WEST);

//        JButton jButton = new JButton("注册");
//        jButton.setBounds(140, 150, 40, 30);
//        jButton.setMargin(new Insets(0, 0, 0, 0));
//        //panel2.add(jButton,BorderLayout.EAST);
//        add(jButton);

        final int[] type = {0};

//        radioButton1.addMouseListener(new MouseAdapter() {
//            @Override
//            public void mouseClicked(MouseEvent e) {
//                if (radioButton1.isSelected()) {
//                    type[0] = 1;
//                }else type[0] = 0;
//
//            }
//        });


        loginButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                String text = jTextField.getText();
                String password = passwordField.getText();
                if (text == null || text.length() <= 0 || password == null || password.length() <= 0) {
                    JOptionPane.showMessageDialog(panel2,
                            "用户名或密码不能为空", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                AwardPunish awardPunish = new AwardPunish();
                awardPunish.setSno(text);
                awardPunish.setAprecord(password);
                AwardPunish one = AwardPunishService.getOne(awardPunish);
                if (one!=null){
                    JOptionPane.showMessageDialog(panel2,
                            "记录已存在", "系统提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                AwardPunishService.insert(awardPunish);
                JOptionPane.showMessageDialog(panel2,
                        "添加成功", "系统提示", JOptionPane.WARNING_MESSAGE);
                return;
            }
        });


//        jButton.addMouseListener(new MouseAdapter() {
//            @Override
//            public void mouseClicked(MouseEvent e) {
//                String text = jTextField.getText();
//                char[] password = passwordField.getPassword();
//                if (text == null || text.length() <= 0 || password == null || password.length <= 0) {
//                    JOptionPane.showMessageDialog(panel2,
//                            "用户名或密码不能为空", "系统提示", JOptionPane.WARNING_MESSAGE);
//                    return;
//                }
//                User user = new User();
//                user.setName(text);
//                user.setPassword(String.valueOf(password));
//                Integer integer = UserService.getAll();
//                user.setId(++integer);
//                //System.out.println(user);
//                user.setType(type[0]);
//                User aNull = UserService.getNull(user);
//                if (aNull != null) {
//                    JOptionPane.showMessageDialog(panel2,
//                            "用户名已存在!", "系统提示", JOptionPane.WARNING_MESSAGE);
//                    return;
//                }
//                UserService.insert(user);
//                JOptionPane.showMessageDialog(panel2,
//                        "注册成功", "系统提示", JOptionPane.WARNING_MESSAGE);
//                //setPanel(new StudentPanel());
//            }
//        });

    }

    public void setPanel(int page) {
        pageSize = page;
        MainJFrame.setPanel(new AwardInsertPanel());
    }

    public void setPanel(JPanel panel) {
        MainJFrame.setPanel(panel);
    }

}
